package com.coderxq.blog.handler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(JSONObject.class)
public class ObjectJsonHandler extends BaseTypeHandler<JSONObject> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType)
			throws SQLException {

		ps.setString(i, String.valueOf(parameter.toString()));
		
	}

	@Override
	public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException {

		String sqlJson = rs.getString(columnName);

		if (null != sqlJson) {

			return JSONUtil.parseObj(sqlJson);

		}
		return null;
	}

	@Override
	public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {

		String sqlJson = rs.getString(columnIndex);

		if (null != sqlJson) {

			return JSONUtil.parseObj(sqlJson);

		}
		
		return null;
	}

	@Override
	public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {

		String sqlJson = cs.getString(columnIndex);

		if (null != sqlJson) {

			return JSONUtil.parseObj(sqlJson);
			
		}
		
		return null;
	}
	
	

}
